//
//  MKUIImagePickerOperation.m
//  mallhelper
//
//  Created by LIKUN on 15/8/4.
//  Copyright (c) 2015年 malllink. All rights reserved.
//

#import "MKUIImagePickerOperation.h"
//#import "KKFileManager.h"

static MKUIImagePickerOperation *imagePickerOperation = nil;

@implementation MKUIImagePickerOperation
//@synthesize imagePickerController;
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imagePickerOperation = [[MKUIImagePickerOperation alloc]init];
    });
    return imagePickerOperation;
}

- (id)init
{
    if (self = [super init]) {

    }
    return self;
}

- (void)showImagePickerControllerWithType:(MediaType)type edit:(BOOL)edit success:(void(^)(UIImagePickerController *imagePickerController))block
{
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
    if (type == 0) {// 拍照
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有检测到摄像头" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
            return;
        }
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    } else if (type == 1){// 从相册选择
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
        imagePickerController.allowsEditing = edit;
    block(imagePickerController);
    
}
- (void)pickerImageFinished:(void(^)(UIImage *originalImage, UIImage *compressionImage))block
{
    self.finishBlock = block;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    
    UIImage *imageTmp = [UIImage new];
    if ([mediaType isEqualToString:@"public.image"]){
        imageTmp = info[UIImagePickerControllerEditedImage];
        if (!imageTmp) {
            imageTmp = info[UIImagePickerControllerOriginalImage];
        }

        NSData *imgData = [QFMultimedia compressTo2K:imageTmp quality:0.6];

        UIImage *image = [UIImage imageWithData:imgData];
        
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
        }
        UIImage *btnImg = [[UIImage alloc] initWithCGImage:[self compressToMin:imageTmp].CGImage];
        NSLog(@"btnImg.size == %@",NSStringFromCGSize(btnImg.size));
        
//        NSData *data = UIImagePNGRepresentation(image);
//        NSData *small_data = UIImagePNGRepresentation(btnImg);
//
//        KKFileManager *manager = [KKFileManager shareInstance];
//        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//        NSString *documentsDirectory = [paths objectAtIndex:0];
//        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"testimage"];
//        NSString *path2 = [documentsDirectory stringByAppendingPathComponent:@"testimage_small"];
//
//        NSLog(@"path == %@",path);
//        [manager writeFilePath:path data:data];
//        [manager writeFilePath:path2 data:small_data];

        if (self.finishBlock) {
            self.finishBlock(image,btnImg);
        }
    }

}
- (UIImage *) compressToMin:(UIImage *)image{
    CGFloat length = 0;
    if (image.size.width > image.size.height) {
        length = image.size.width;
    } else {
        length = image.size.height;
    }
//    NSLog(@"image.size.width == %f  image.size.height == %f",image.size.width,image.size.height);
    CGFloat ratio = 1;
    if (length > 140) {
        ratio = 140.0/length;
    }
    return [QFMultimedia compress:image ratio:ratio];
}

@end
